﻿using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;

namespace Chevron.OrderDetails.Models
{
    public class OrderDto : IComparable<OrderDto>
    {
        public OrderDto()
        {
            Tests = new List<OrderDetailDto>();
        }

        public DateTime OrderDateTime { get; set; }

        public string OrderNum { get; set; }

        public int InvoiceNum { get; set; }

        public string PatientId { get; set; }

        // Patient

        public string PatientPrefix { get; set; }

        public string PatientName { get; set; }

        public string PatientAge { get; set; }

        public Gender PatientSex { get; set; }

        public string PatientPhone { get; set; }

        // Physician

        public PhysicianDto Physician { get; set; }

        // Tests

        public List<OrderDetailDto> Tests { get; set; }

        // Invoice

        public decimal GrossBill { get; set; }

        public decimal Discount { get; set; }

        public decimal NetBill { get; set; }

        public decimal Payment { get; set; }

        public decimal Due { get; set; }

        // Referral

        public decimal GrossReferral { get; set; }

        public decimal NetReferral { get; set; }

        #region IComparable<OrderDto> Members

        public int CompareTo(OrderDto other)
        {
            return InvoiceNum.CompareTo(other.InvoiceNum);
        }

        #endregion

        public void Deserialize(JObject obj, PhysiciansCatalog physicians, TestCatalog catalog)
        {
            InvoiceNum = ((string)obj[JsonFields.InvoiceNum]).ToInt();
            OrderNum = (string)obj[JsonFields.OrderNum];
            PatientId = (string) obj[JsonFields.PatientId];
            OrderDateTime = Utilities.ParseJsonDateTime((string) obj[JsonFields.CreateDate],
                                                        (string) obj[JsonFields.CreateTime]);

            foreach (JObject testObj in (JArray) obj[JsonFields.Test])
            {
                try
                {
                    var testDetail = new OrderDetailDto();
                    testDetail.Deserialize(testObj, catalog);
                    Tests.Add(testDetail);
                }
                catch (Exception)
                {
                }
            }

            Physician = physicians.GetPhysician((string) obj[JsonFields.PhysicianUPIN],
                                                (string) obj[JsonFields.PhysicianName]);

            var invoice = (JObject) obj[JsonFields.Invoice];
            GrossBill = ((string) invoice[JsonFields.GrossBill]).ToDecimal();
            Discount = ((string) invoice[JsonFields.Discount]).ToDecimal();
            NetBill = ((string) invoice[JsonFields.NetBill]).ToDecimal();
            Payment = ((string) invoice[JsonFields.TotalPayment]).ToDecimal();
            Due = ((string) invoice[JsonFields.TotalDue]).ToDecimal();
        }
    }
}